// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

const EMPTY_STATE = {
  MAP: {},
  DICT: [],
  list: [],
  one: {},
  param: {},
  total: 0,
  visible: false,
  modal: '',
}

// ============================== 导出混合 ============================== //

export default {
  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 前缀
     */
    pre() {
      return this.path ? `${this.path}/` : ''
    },

    /**
     * 模块的状态集
     */
    state() {
      const emptyState = CORE.clone(EMPTY_STATE)
      if (!this.$store) return emptyState

      const pathList = this.path ? this.path.split('/') : []
      let state = this.$store.state
      for (const path of pathList) {
        if (!CORE.isJSON(state[path])) return emptyState
        state = state[path]
      }
      return state
    },

    /**
     * 样式类
     */
    className() {
      return `CoreWeb-${this.$options.name}`
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 模块操作方法
     */
    commit(method, data) {
      if (this.$store) this.$store.commit(`${this.pre}${method}`, data)
    },

    /**
     * 获取尺寸
     */
    getSize(size) {
      if (!size) return null
      return /^\d+(\.\d+)?$/.test(size) ? `${size}px` : size
    },
  },
}
